
<?php
	include "../php/HeaderBlock.php";
	$header = &New HeaderBlock;
	$header->highlighted="events";
	$header->print_head();
require_once 'autoload.php';
	
?>

<div class="content">
<h2>Demonstration code</h2>

<?php
function replace_plain_text_link($plain_text) {
	$url_html = preg_replace(
	'/(?<!S)((http(s?):\/\/)|(www.))+([\w.1-9\&=#?\-~+,%;\/]+)/',
	'<a href="http$3://$4$5">http$3://$4$5</a>', $plain_text);
	return ($url_html);
}
$number = $_GET["id"];

$username="calendar";
$password="pw4sn6";
$database="events";

//mysql_connect(localhost,$username,$password) or die( "Unable to connect");
//@mysql_select_db($database) or die( "Unable to select database");

	$cdClient = new Slash_Client_Client("http://saraha.jackpot.uk.net:82", "oarc", "ocset");
	$propreq = array(
		"{http://theoarc.org.uk/events}keyholder",
		"{http://theoarc.org.uk/events}contact",
		"{DAV:}getetag",
 		"{urn:ietf:params:xml:ns:caldav}calendar-data"
	);
	$href = $number;
	$results = $cdClient->getEvent($href, $propreq);
  $calobj = $results[$href]["caldata"];
  $events = $calobj->getEvents();
	$event = $events[0];
  $startDate = $event->getDtStart()->format(DATE_RFC822);
  $endDate = $event->getDtEnd()->format(DATE_RFC822);
  $title = $event->getSummary();
  $details = $event->getDescription();
  $location = $event->getLocation();
  $props = $results[$href]["props"];
  print "<!--";
  var_dump($props);
  print " -->\n";
  $propName = "{http://theoarc.org.uk/events}keyholder";
  if (isset($props[$propName]))
  {
  	$keyholder = $props[$propName]->textContent;
  } 
  $propName = "{http://theoarc.org.uk/events}contact";
  if (isset($props[$propName]))
  {
  	$contact = $props[$propName]->textContent;
  }
  print "<!--Properties: $keyholder, $contact-->\n"; 
    $n = 1;

if ($n != 0) {
  echo "<h2>" . $title . "</h2>";
  print "$startDate - $endDate";
  //if ($endtime != "00:00:00") echo date("-g:ia",strtotime("2005-01-01 " . $endtime));
  if ($location != "Whichever space is free" && $location != "") echo " - " . htmlentities($location);
  echo "<br />\n";
  if ($contact != "") echo "Contact: " . htmlentities($contact) . "<br />";
  if ($details != "") echo "<br />" . replace_plain_text_link(nl2br(htmlentities($details))) . "<br />\n";

  if ($keyholder)
    echo "<b>(event confirmed; keyholder is: " . htmlentities($keyholder) . ")</b>";
  else
    echo "<b>(event unconfirmed; a keyholder is needed)</b>";

} else {
  echo "No such entry found in database!<br /><br />";
}

?>

<br />

<div class="Ftr"><a href="index.php">Back to Calendar</a></div>

</div>


</body>

</html>
